在上篇中,我們是需要到 Line Notify 登入後的個人介面發行 token,但總不能叫每一個加入 line bot 的好友都自己去註冊 Line Notify 後再跟你說 token,因此我們可以透過程式碼
@app.route("/callback/notify", methods=['GET'])
def notify():
code = request.args.get('code')
print(code)
#抓取user的notify token
user_notify_token_get_url = 'https://notify-bot.line.me/oauth/token'
params = {
'grant_type':'authorization_code',
'code':code,
'redirect_uri':'<https://ngrok domain/notify>',
'client_id':'<Notify client_id>',
'client_secret':'<Notify client_secret>'
}
get_token = requests.post(user_notify_token_get_url,params=params)
print(get_token.json())
token = get_token.json()['access_token']
print(token)
#抓取user的info
user_info_url = 'https://notify-api.line.me/api/status'
headers = {'Authorization':'Bearer '+ token}
get_user_info = requests.get(user_info_url,headers=headers)
print(get_user_info.json())